package main;

import java.util.logging.Logger;

import mobile.MobileManager;

import comm.Message;

import display.DisplayManager;

/** Main class of the framework.
 * Manages all the connections with the mobile devices and the display.
 * 
 * @author Bruno Zumba (03/10/2011)
 *
 */
public class Manager {
	//Debugging
	private Logger logger;
	
	public static final int CONNECT = 0;
	public static final int DISCONNECT = 1;
	
	public static final int MOBILE = 0;
	public static final int DISPLAY = 1;
	
	private static DisplayManager displayManager;
	private static MobileManager mobileManager;
		
	public Manager(){
		//Set-up debugging
		logger = Logger.getLogger("FrameWork.Manager");
		
		displayManager = new DisplayManager();
		mobileManager = new MobileManager();
		mobileManager.start();
		mobileManager.setRunning(true);
	}
	
	public static void redirectMessage(Message msg, int from){
		//Check the receiver field of the Message, search it in the array of IP and send the message.
		switch(from){
		case MOBILE:
			if (msg.getType() == DISCONNECT){//A mobile socket disconnected
				mobileManager.checkConnectedDevices();	
				
			} 
				displayManager.send(msg.getMsg());				
			
			break;
		case DISPLAY:
			if (msg.getType() == DISCONNECT){//The display disconnected
				//Fodeu
			} else {
				mobileManager.send(msg.getMsg(), msg.getReceiver());
			}
			break;
		}
	}
}

